package com.zydl.f9164m.util;

import android.content.Context;

import com.clj.fastble.utils.BleLog;
import com.clj.fastble.utils.HexUtil;
import com.zydl.f9164m.R;
import com.zydl.f9164m.basis.BasisApp;
import com.zydl.f9164m.bean.ParamListData;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cz.msebera.android.httpclient.util.TextUtils;

public class PassWayUtil {


    /**
     * 通道属性
     */
    static String sDeviceInface; //硬件接口
    static String sChannelOnOff;// 数据采样开关
    static String sPowerOnDelay;//上电延时
    static String sFactorName;//要素标识  通道标识
    static String sUnit;//单位          采集数据单位
    static String sUnitSize;//采集数据大小
    static String sStorageTimeInterval;//存储时间间隔
    static String sPrecision;//精度     采集数据精度
    static String sSampTimeInterval;//采样间隔
    static String sUpReportTime;//上报间隔
    static String awakeningInterval = "";//唤醒间隔
    static String exceptionValue = "";//异常值阀值
    static String alarmCaptureInterval = "";//报警抓拍间隔
    static String sSampBaseValueMethod;//基值使用方法
    static String sSampBaseValue;//采样基值


    /**
     * 报警参数
     */
    static String upperLimitValue1;//上限值
    static String upperLimitValue2;//上限值
    static String upperLimitValue3;//上限值
    static String upperLimitValueOfDo = "0";//上限值关联DO编码
    static String upperLimitValueConnect = "";//上限值报警内容
    static String lowerLimitValue1;//下限值1
    static String lowerLimitValue2;//下限值2
    static String lowerLimitValue3;//下限值3
    static String lowerLimitValueOfDo = "0";//下限值关联DO编码
    static String lowerLimitValueConnect = "";//下限值报警内容
    static String threshold = "";//报警阈值
    static String sDoRelate = "0";//阈值DO关联
    static String sAlarmContext;//报警内容
    static String reportingInterval1 = "";//加报间隔1
    static String reportingInterval2 = "";//加报间隔2
    static String reportingInterval3 = "";//加报间隔3
    static String acquisitionInterval1 = "";//采集间隔1
    static String acquisitionInterval2 = "";//采集间隔2
    static String acquisitionInterval3 = "";//采集间隔3
    static String sensorMaxRange;//传感器上量程
    static String sensorMinRange;//传感器下量程

    /**
     * /通信参数
     */
    //RS系列
    static String sConnMode;//总线通信模式
    static String sSerial_Baudrate;   //波特率
    static String sSerial_Com;     //数据位sSerial_Com.substring(0, 1)     停止位sSerial_Com.substring(2, 3)    校验位sSerial_Com.substring(1, 2)
    //总线通信模式MODBUS
    static String sMODBUS_deviceAdd;          //设备地址
    static String sMODBUS_functionCode;       //功能码
    static String sMODBUS_registerAdd;           //寄存器地址
    static String sMODBUS_registerNumber;         //寄存器个数
    static String sMODBUS_dataIntercept;         //数据截取
    static String sMODBUS_universalSerialPort;         //通用串口

    //ADC系列
    static String sElectricity_sensor;//电流传感器       ADC类型
    static String sMaxRange;//上量程
    static String sMinRange;//下量程
    static String sMaxValue;//最大值  输入电流
    static String sMinValue;//最小值  输入电流


    /**
     * 扩充
     */
    static String dataBits;     // 数据位
    static String stopBits;     // 停止位
    static String checkBits;     // 校验位
    static String diAlarm;      //报警触发方式


    /**
     * @param sMsgObjString at+show的内容
     * @param context
     * @param msgIndexOfMsg 通道属性的关键字
     * @param msgIndexOfCom 通信参数的关键字
     * @param dataList      数据都在添加在这个list里面
     */
    public static void initPassWayData(String sMsgObjString, Context context, String msgIndexOfMsg, String msgIndexOfCom, String msgIndexOfFilter, String msgIndexOfCrc, List<ParamListData> dataList, boolean isChange) {

        sDeviceInface = ""; //硬件接口
        sChannelOnOff = "";// 数据采样开关
        sFactorName = "";//要素标识  通道标识
        sSampBaseValueMethod = "";//基值使用方法
        sSampBaseValue = "";//采样基值
        sUnit = "";//单位          采集数据单位
        sPrecision = "";//精度     采集数据精度
        sUnitSize = "";//采集数据大小
        sPowerOnDelay = "";//上电延时
        sStorageTimeInterval = "";//存储时间间隔
        sSampTimeInterval = "";//采样间隔
        sUpReportTime = "";//上报间隔
        sConnMode = "";//总线通信模式
        sSerial_Baudrate = "";   //波特率
        sSerial_Com = "";     //数据位sSerial_Com.substring(0, 1)     停止位sSerial_Com.substring(2, 3)    校验位sSerial_Com.substring(1, 2)
        sMODBUS_deviceAdd = "";          //设备地址
        sMODBUS_functionCode = "";       //功能码
        sMODBUS_registerAdd = "";           //寄存器地址
        sMODBUS_registerNumber = "";         //寄存器个数
        sMODBUS_dataIntercept = "";         //数据截取
        sMODBUS_universalSerialPort = "";         //通用串口
        sElectricity_sensor = "";//电流传感器       ADC类型
        sMaxRange = "";//上量程
        sMinRange = "";//下量程
        sMaxValue = "";//最大值  输入电流
        sMinValue = "";//最小值  输入电流
        upperLimitValue1 = "";//上限值1
        upperLimitValue2 = "";//上限值2
        upperLimitValue3 = "";//上限值3
        upperLimitValueConnect = "";     //上限值报警内容
        upperLimitValueOfDo = "";     //上限值关联DO编码
        lowerLimitValue1 = "";//下限值1
        lowerLimitValue2 = "";//下限值2
        lowerLimitValue3 = "";//下限值3
        lowerLimitValueOfDo = "";     //下限值关联DO编码
        lowerLimitValueConnect = "";     //下限值报警内容

        threshold = "";//报警阈值
        diAlarm = "";     //报警触发方式
        sAlarmContext = "";//报警内容
        sDoRelate = "0";     //阈值DO关联

        dataBits = "";     // 数据位
        stopBits = "";     // 停止位
        checkBits = "";     // 校验位

        reportingInterval1 = "";//加报间隔1
        reportingInterval2 = "";//加报间隔2
        reportingInterval3 = "";//加报间隔3
        acquisitionInterval1 = "";//采集间隔1
        acquisitionInterval2 = "";//采集间隔2
        acquisitionInterval3 = "";//采集间隔3

        sensorMaxRange = "";//传感器上量程
        sensorMinRange = "";//传感器下量程

        int iIndexHW_Chn1 = sMsgObjString.indexOf(msgIndexOfMsg);
        if (iIndexHW_Chn1 != -1) {
            String sHW_Chn1 = SubstringUtil.subString(sMsgObjString, msgIndexOfMsg);
            if (sHW_Chn1 != null && sHW_Chn1.trim().length() > 0) {
                String[] splitHW_Chn1 = sHW_Chn1.split(",");
                if (splitHW_Chn1.length == 35) {
                    sDeviceInface = splitHW_Chn1[0]; //硬件接口
                    sChannelOnOff = splitHW_Chn1[1];// 通道开关  数据采样开关
                    sPowerOnDelay = splitHW_Chn1[2];//上电延时
                    sFactorName = splitHW_Chn1[3];//要素标识  通道标识
                    sUnit = splitHW_Chn1[4];//单位   采集数据单位
                    sUnitSize = splitHW_Chn1[5];//元素单元大小  采集数据大小
                    sStorageTimeInterval = splitHW_Chn1[6];//存储时间间隔
                    sPrecision = splitHW_Chn1[7];//数据采集精度
                    sSampTimeInterval = splitHW_Chn1[8];//数据采样间隔
                    sUpReportTime = splitHW_Chn1[9];//上报时间  数据上报间隔
                    awakeningInterval = splitHW_Chn1[10];//唤醒间隔
                    exceptionValue = splitHW_Chn1[11];//异常值阀值
                    alarmCaptureInterval = splitHW_Chn1[12];//报警抓拍间隔
                    upperLimitValue1 = splitHW_Chn1[13];//上限值1
                    upperLimitValue2 = splitHW_Chn1[14];//上限值2
                    upperLimitValue3 = splitHW_Chn1[15];//上限值3
                    upperLimitValueOfDo = splitHW_Chn1[16];//上限值关联DO编码
                    upperLimitValueConnect = splitHW_Chn1[17];//上限值报警内容
                    lowerLimitValue1 = splitHW_Chn1[18];//下限值
                    lowerLimitValue2 = splitHW_Chn1[19];//下限值
                    lowerLimitValue3 = splitHW_Chn1[20];//下限值
                    lowerLimitValueOfDo = splitHW_Chn1[21];//下限值关联DO编码
                    lowerLimitValueConnect = splitHW_Chn1[22];//下限值报警内容
                    threshold = splitHW_Chn1[23];//报警阈值
                    sDoRelate = splitHW_Chn1[24];//阈值DO关联
                    sAlarmContext = splitHW_Chn1[25];//报警内容
                    sSampBaseValue = splitHW_Chn1[26];//采样基值
                    reportingInterval1 = splitHW_Chn1[27];//加报间隔1
                    reportingInterval2 = splitHW_Chn1[28];//加报间隔2
                    reportingInterval3 = splitHW_Chn1[29];//加报间隔3
                    acquisitionInterval1 = splitHW_Chn1[30];//采集间隔1
                    acquisitionInterval2 = splitHW_Chn1[31];//采集间隔2
                    acquisitionInterval3 = splitHW_Chn1[32];//采集间隔3
                    sensorMaxRange = splitHW_Chn1[33];//传感器上量程
                    sensorMinRange = splitHW_Chn1[34];//传感器下量程
                } else if (splitHW_Chn1.length == 4) {
                    sDeviceInface = splitHW_Chn1[0]; //硬件接口
                    diAlarm = splitHW_Chn1[1];// 报警触发方式
                    sDoRelate = splitHW_Chn1[2];//阈值DO关联
                    sAlarmContext = splitHW_Chn1[3];//报警内容
                }

                //todo   标记5
            }
        }
        //todo   标记5
        dataList.add(new ParamListData("硬件接口", 2, "" + sDeviceInface, "", "", false, isChange));
        List<String> openOrClose = Arrays.asList(context.getResources().getStringArray(R.array.openOrClose));
        dataList.add(new ParamListData("数据采样开关", 1, "" + getKeyFromList(sChannelOnOff, openOrClose), openOrClose, "", "", false, isChange));
        dataList.add(new ParamListData("通道标识", 2, "" + sFactorName, "", "", true, isChange));
        List<String> baseValueMethodList = Arrays.asList(context.getResources().getStringArray(R.array.baseValueMethod));
        if (sSampBaseValue.contains("-")) {
            dataList.add(new ParamListData("基值使用方法", 1, "基值-采集值", baseValueMethodList, "", "", false, isChange));
        } else if (sSampBaseValue.contains("@")){
            dataList.add(new ParamListData("基值使用方法", 1, "采集值-基值", baseValueMethodList, "", "", false, isChange));
        }else {
            dataList.add(new ParamListData("基值使用方法", 1, "采集值+基值", baseValueMethodList, "", "", false, isChange));
        }
        dataList.add(new ParamListData("采样基值", 2, "" + sSampBaseValue.replace("-", ""), "", "", true, isChange));
        List<String> unitList = Arrays.asList(context.getResources().getStringArray(R.array.unit));
        dataList.add(new ParamListData("采集数据单位", 1, "" + sUnit, unitList, "", "", true, isChange));
        List<String> sPrecisionList = Arrays.asList(context.getResources().getStringArray(R.array.precision));
        dataList.add(new ParamListData("采集数据精度", 1, "" + sPrecision, sPrecisionList, "", "", true, isChange));
        List<String> unitSizeList = Arrays.asList(context.getResources().getStringArray(R.array.unitSize));
        dataList.add(new ParamListData("上电延迟", 2, "" + sPowerOnDelay, "", "(0~255s)", true, isChange));
        dataList.add(new ParamListData("数据存储间隔", 2, "" + sStorageTimeInterval, "", "(min)", true, isChange));
        dataList.add(new ParamListData("数据采样间隔", 2, "" + sSampTimeInterval, "", "(5~65535s)", true, isChange));
        dataList.add(new ParamListData("数据上报间隔", 2, "" + sUpReportTime, "", "(0~65535min)", true, isChange));
        dataList.add(new ParamListData("唤醒间隔", 2, "" + awakeningInterval, "", "(s)", true, isChange));
        dataList.add(new ParamListData("异常值阀值", 2, "" + exceptionValue, "", "", true, isChange));

        dataList.add(new ParamListData("报警阈值", 2, "" + threshold, "", "", true, isChange));
        dataList.add(new ParamListData("加报间隔1", 2, "" + reportingInterval1, "", "", true, isChange));
        dataList.add(new ParamListData("加报间隔2", 2, "" + reportingInterval2, "", "", true, isChange));
        dataList.add(new ParamListData("加报间隔3", 2, "" + reportingInterval3, "", "", true, isChange));
        dataList.add(new ParamListData("采集间隔1", 2, "" + acquisitionInterval1, "", "", true, isChange));
        dataList.add(new ParamListData("采集间隔2", 2, "" + acquisitionInterval2, "", "", true, isChange));
        dataList.add(new ParamListData("采集间隔3", 2, "" + acquisitionInterval3, "", "", true, isChange));
        dataList.add(new ParamListData("传感器上量程", 2, "" + sensorMaxRange, "", "", true, isChange));
        dataList.add(new ParamListData("传感器下量程", 2, "" + sensorMinRange, "", "", true, isChange));
        dataList.add(new ParamListData("报警抓拍间隔", 2, "" + alarmCaptureInterval, "", "", true, isChange));
        dataList.add(new ParamListData("上限值1", 2, "" + upperLimitValue1, "", "", true, isChange));
        dataList.add(new ParamListData("上限值2", 2, "" + upperLimitValue2, "", "", true, isChange));
        dataList.add(new ParamListData("上限值3", 2, "" + upperLimitValue3, "", "", true, isChange));
        dataList.add(new ParamListData("上限值报警内容", 2, "" + upperLimitValueConnect, "", "", true, isChange));
        dataList.add(new ParamListData("上限值关联DO", 2, "" + upperLimitValueOfDo, "", "", true, isChange));
        switch (upperLimitValueOfDo) {
            default:
//                    case "0":
                dataList.add(new ParamListData("上限值DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO2", 5, "0", "", "", false, isChange));
                break;
            case "1":
                dataList.add(new ParamListData("上限值DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO2", 5, "0", "", "", false, isChange));
                break;
            case "2":
                dataList.add(new ParamListData("上限值DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO2", 5, "0", "", "", false, isChange));
                break;
            case "3":
                dataList.add(new ParamListData("上限值DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO2", 5, "0", "", "", false, isChange));
                break;
            case "4":
                dataList.add(new ParamListData("上限值DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO2", 5, "4", "", "", false, isChange));
                break;
            case "5":
                dataList.add(new ParamListData("上限值DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO2", 5, "4", "", "", false, isChange));
                break;
            case "6":
                dataList.add(new ParamListData("上限值DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO2", 5, "4", "", "", false, isChange));
                break;
            case "7":
                dataList.add(new ParamListData("上限值DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("上限值DO2", 5, "4", "", "", false, isChange));
                break;
        }

        dataList.add(new ParamListData("下限值1", 2, "" + lowerLimitValue1, "", "", true, isChange));
        dataList.add(new ParamListData("下限值2", 2, "" + lowerLimitValue2, "", "", true, isChange));
        dataList.add(new ParamListData("下限值3", 2, "" + lowerLimitValue3, "", "", true, isChange));
        dataList.add(new ParamListData("下限值报警内容", 2, "" + lowerLimitValueConnect, "", "", true, isChange));
        dataList.add(new ParamListData("下限值关联DO", 2, "" + lowerLimitValueOfDo, "", "", true, isChange));
        switch (lowerLimitValueOfDo) {
            default:
//                    case "0":
                dataList.add(new ParamListData("下限值DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO1", 5, "0", "", "", false, isChange));
                break;
            case "1":
                dataList.add(new ParamListData("下限值DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO2", 5, "0", "", "", false, isChange));
                break;
            case "2":
                dataList.add(new ParamListData("下限值DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO2", 5, "0", "", "", false, isChange));
                break;
            case "3":
                dataList.add(new ParamListData("下限值DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO2", 5, "0", "", "", false, isChange));
                break;
            case "4":
                dataList.add(new ParamListData("下限值DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO2", 5, "4", "", "", false, isChange));
                break;
            case "5":
                dataList.add(new ParamListData("下限值DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO2", 5, "4", "", "", false, isChange));
                break;
            case "6":
                dataList.add(new ParamListData("下限值DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO2", 5, "4", "", "", false, isChange));
                break;
            case "7":
                dataList.add(new ParamListData("下限值DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("下限值DO2", 5, "4", "", "", false, isChange));
                break;
        }
        List<String> diAlarmList = Arrays.asList(context.getResources().getStringArray(R.array.diAlarm));
        dataList.add(new ParamListData("报警触发方式", 1, "" + getDiAlarm(diAlarm), diAlarmList, "", "", false, isChange));
        dataList.add(new ParamListData("报警内容", 2, "" + sAlarmContext, "AT+CATXT", "", true, isChange));
        dataList.add(new ParamListData("阈值DO关联", 2, "" + sDoRelate, "", "", true, isChange));
        switch (sDoRelate) {
            default:
//                    case "0":
                dataList.add(new ParamListData("报警DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO2", 5, "0", "", "", false, isChange));
                break;
            case "1":
                dataList.add(new ParamListData("报警DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO2", 5, "0", "", "", false, isChange));
                break;
            case "2":
                dataList.add(new ParamListData("报警DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO2", 5, "0", "", "", false, isChange));
                break;
            case "3":
                dataList.add(new ParamListData("报警DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO2", 5, "0", "", "", false, isChange));
                break;
            case "4":
                dataList.add(new ParamListData("报警DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO2", 5, "4", "", "", false, isChange));
                break;
            case "5":
                dataList.add(new ParamListData("报警DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO1", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO2", 5, "4", "", "", false, isChange));
                break;
            case "6":
                dataList.add(new ParamListData("报警DO0", 5, "0", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO2", 5, "4", "", "", false, isChange));
                break;
            case "7":
                dataList.add(new ParamListData("报警DO0", 5, "1", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO1", 5, "2", "", "", false, isChange));
                dataList.add(new ParamListData("报警DO2", 5, "4", "", "", false, isChange));
                break;
        }


        int iIndexCH1_Com_Cfg = sMsgObjString.indexOf(msgIndexOfCom);
        if (iIndexCH1_Com_Cfg != -1) {
            String sCH1_Com_Cfg = SubstringUtil.subString(sMsgObjString, msgIndexOfCom);
            if (sCH1_Com_Cfg.startsWith("RS")) {
                sConnMode = "";
                String[] splitCH1_Com_Cfg = sCH1_Com_Cfg.split(",");
                if (splitCH1_Com_Cfg.length >= 4) {
                    sSerial_Baudrate = splitCH1_Com_Cfg[1]; //波特率
                    sSerial_Com = splitCH1_Com_Cfg[2]; //数据位 停止位校验位
                    sConnMode = splitCH1_Com_Cfg[3];//通信模式
                    if (splitCH1_Com_Cfg.length == 8) {
                        sMODBUS_deviceAdd = splitCH1_Com_Cfg[4];  //设备地址
                        sMODBUS_functionCode = splitCH1_Com_Cfg[5];//功能码
                        sMODBUS_registerAdd = splitCH1_Com_Cfg[6];//寄存器地址
                        sMODBUS_registerNumber = splitCH1_Com_Cfg[7];//寄存器个数
                    } else if (splitCH1_Com_Cfg.length == 5) {
                        sMODBUS_universalSerialPort = splitCH1_Com_Cfg[4];  //通用串口
                    }
                }
                //todo   标记1

            } else if (sCH1_Com_Cfg.startsWith("ADC")) {
                String[] splitCH1_Com_Cfg = sCH1_Com_Cfg.split(",");
                sElectricity_sensor = splitCH1_Com_Cfg[1];//ADC类型
                sMaxRange = splitCH1_Com_Cfg[2];//最大量程
                sMinRange = splitCH1_Com_Cfg[3];//最小量程
                sMaxValue = splitCH1_Com_Cfg[4];//最大值
                sMinValue = splitCH1_Com_Cfg[5];//最小值
                if (sElectricity_sensor.equals("1")) {
                    sElectricity_sensor = "电流";
                } else {
                    sElectricity_sensor = "电压";
                }
                //todo   标记2
            }
        }

        //todo 标记1
        List<String> rsBaudRateList = Arrays.asList(context.getResources().getStringArray(R.array.rsBaudRate));
        dataList.add(new ParamListData("通信波特率", 1, "" + sSerial_Baudrate, rsBaudRateList, "", "", true, isChange));
        if (sSerial_Com.length() == 3) {
            dataBits = sSerial_Com.substring(0, 1);     //数据位
            stopBits = sSerial_Com.substring(2, 3);      //停止位
            checkBits = sSerial_Com.substring(1, 2);       //校验位
            switch (checkBits) {
                case "N":
                    checkBits = "无校验";
                    break;
                case "O":
                    checkBits = "奇校验";
                    break;
                case "E":
                    checkBits = "偶校验";
                    break;
            }
        }
        //数据位
        List<String> rsDataBitsList = Arrays.asList(context.getResources().getStringArray(R.array.rsDataBits));
        dataList.add(new ParamListData("数据位", 1, "" + dataBits + "位", rsDataBitsList, "", "", false, isChange));
        //停止位
        List<String> rsStopBitsList = new ArrayList<>();
        rsStopBitsList.add("1位");
        dataList.add(new ParamListData("停止位", 1, "" + stopBits + "位", rsStopBitsList, "", "", false, isChange));
        //校验位
        List<String> checkBitsList = Arrays.asList(context.getResources().getStringArray(R.array.checkBits));
        dataList.add(new ParamListData("校验位", 1, "" + checkBits, checkBitsList, "", "", false, isChange));
        List<String> rsModelList = Arrays.asList(context.getResources().getStringArray(R.array.rsModel));
        dataList.add(new ParamListData("总线通信模式", 1, "" + getKeyFromList(sConnMode, rsModelList), rsModelList, "", "", true, false));

        int iIndexFilter_Com_Cfg = sMsgObjString.indexOf(msgIndexOfFilter);
        if (iIndexFilter_Com_Cfg != -1) {
            String dataFilter = SubstringUtil.subString(sMsgObjString, msgIndexOfFilter);
            sMODBUS_dataIntercept = dataFilter;  ////数据截取
        }
        PassWayUtil.initModBus("" + sConnMode, dataList, sMODBUS_deviceAdd, sMODBUS_functionCode, sMODBUS_registerAdd, sMODBUS_registerNumber, sMODBUS_dataIntercept, sMODBUS_universalSerialPort, isChange);

        //todo   标记2
        List<String> adcList = Arrays.asList(context.getResources().getStringArray(R.array.adcType));
        dataList.add(new ParamListData("ADC类型", 1, "" + sElectricity_sensor, adcList, "", "", false, isChange));
        dataList.add(new ParamListData("上量程", 2, "" + sMaxRange, "", "", true, isChange));
        dataList.add(new ParamListData("下量程", 2, "" + sMinRange, "", "", true, isChange));
        dataList.add(new ParamListData("最大值", 2, "" + sMaxValue, "", "", true, isChange));
        dataList.add(new ParamListData("最小值", 2, "" + sMinValue, "", "", true, isChange));


    }

    /**
     * 获取报警触发方式
     *
     * @return
     */
    private static String getDiAlarm(String diAlarm) {
        if (diAlarm.equals("0")) {
            return "关闭";
        }
        if (diAlarm.equals("1")) {
            return "低电平";
        }
        if (diAlarm.equals("2")) {
            return "高电平";
        }
        return "关闭";
    }

    /**
     * 实例化  通信参数
     *
     * @param sConnMode                   总线通信模式
     * @param dataList                    所有数据的list
     * @param sMODBUS_deviceAdd           设备地址
     * @param sMODBUS_functionCode        设备地址
     * @param sMODBUS_registerAdd         寄存器地址
     * @param sMODBUS_registerNumber      寄存器个数
     * @param sMODBUS_dataFilter          数据截取
     * @param sMODBUS_universalSerialPort 通用串口
     */
    public static void initModBus(String sConnMode, List<ParamListData> dataList, String sMODBUS_deviceAdd, String sMODBUS_functionCode, String sMODBUS_registerAdd, String sMODBUS_registerNumber, String sMODBUS_dataFilter, String sMODBUS_universalSerialPort, boolean isChange) {
        switch (sConnMode) {
            case "1":
                sConnMode = "MODBUS";
                dataList.add(new ParamListData("设备地址", 2, "" + sMODBUS_deviceAdd, "", "", true, isChange));
                dataList.add(new ParamListData("功能码", 2, "" + sMODBUS_functionCode, "", "", true, isChange));
                dataList.add(new ParamListData("寄存器地址", 2, "" + sMODBUS_registerAdd, "", "", true, isChange));
                dataList.add(new ParamListData("寄存器个数", 2, "" + sMODBUS_registerNumber, "", "", true, isChange));
                dataList.add(new ParamListData("数据截取", 2, "" + sMODBUS_dataFilter, "", "", true, isChange));
                dataList.add(new ParamListData("通用串口", 2, "" + sMODBUS_universalSerialPort, "", "", false, isChange));
                break;
            case "2":
                sConnMode = "串口";
                dataList.add(new ParamListData("设备地址", 2, "", "", "", false, isChange));
                dataList.add(new ParamListData("功能码", 2, "", "", "", false, isChange));
                dataList.add(new ParamListData("寄存器地址", 2, "", "", "", false, isChange));
                dataList.add(new ParamListData("寄存器个数", 2, "", "", "", false, isChange));
                dataList.add(new ParamListData("数据截取", 2, "", "", "", false, isChange));
                dataList.add(new ParamListData("通用串口", 2, "" + sMODBUS_universalSerialPort, "", "", true, isChange));
                break;
            case "3":
                sConnMode = "MODBUS_FLOAT";
                dataList.add(new ParamListData("设备地址", 2, "" + sMODBUS_deviceAdd, "", "", true, isChange));
                dataList.add(new ParamListData("功能码", 2, "" + sMODBUS_functionCode, "", "", true, isChange));
                dataList.add(new ParamListData("寄存器地址", 2, "" + sMODBUS_registerAdd, "", "", true, isChange));
                dataList.add(new ParamListData("寄存器个数", 2, "" + sMODBUS_registerNumber, "", "", true, isChange));
                dataList.add(new ParamListData("数据截取", 2, "" + sMODBUS_dataFilter, "", "", true, isChange));
                dataList.add(new ParamListData("通用串口", 2, "" + sMODBUS_universalSerialPort, "", "", false, isChange));
                break;
            case "4":
                sConnMode = "MODBUS_BCD";
                dataList.add(new ParamListData("设备地址", 2, "" + sMODBUS_deviceAdd, "", "", true, isChange));
                dataList.add(new ParamListData("功能码", 2, "" + sMODBUS_functionCode, "", "", true, isChange));
                dataList.add(new ParamListData("寄存器地址", 2, "" + sMODBUS_registerAdd, "", "", true, isChange));
                dataList.add(new ParamListData("寄存器个数", 2, "" + sMODBUS_registerNumber, "", "", true, isChange));
                dataList.add(new ParamListData("数据截取", 2, "" + sMODBUS_dataFilter, "", "", true, isChange));
                dataList.add(new ParamListData("通用串口", 2, "" + sMODBUS_universalSerialPort, "", "", false, isChange));
                break;
            default:
//            case "0":
                sConnMode = "透传";
                dataList.add(new ParamListData("设备地址", 2, "", "", "", false, isChange));
                dataList.add(new ParamListData("功能码", 2, "", "", "", false, isChange));
                dataList.add(new ParamListData("寄存器地址", 2, "", "", "", false, isChange));
                dataList.add(new ParamListData("寄存器个数", 2, "", "", "", false, isChange));
                dataList.add(new ParamListData("数据截取", 2, "", "", "", false, isChange));
                dataList.add(new ParamListData("通用串口", 2, "" + sMODBUS_universalSerialPort, "", "", false, isChange));
                break;
        }
    }


    public static String getDeviceInface(List<ParamListData> passWayList) {
        String deviceInface = "";
        for (ParamListData data : passWayList) {
            if (data.getName().equals("硬件接口")) {
                deviceInface = data.getValue();
            }
        }
        return deviceInface;
    }

    private static String getKeyFromList(String key, List<String> stringList) {
        String value = "";
        if (!TextUtils.isEmpty(key)) {
            value = stringList.get(Integer.valueOf(key));
        }
        return value;
    }

    public static boolean getSubmitList(String sPassageWay, String hardWareInterface, List<ParamListData> passWayList, List<ParamListData> submitList) throws UnsupportedEncodingException {

        sDeviceInface = ""; //硬件接口
        sChannelOnOff = "";// 数据采样开关
        sFactorName = "";//要素标识  通道标识
        sSampBaseValueMethod = "";//基值使用方法
        sSampBaseValue = "";//采样基值
        sUnit = "";//单位          采集数据单位
        sPrecision = "";//精度     采集数据精度
        sPowerOnDelay = "";//上电延时
        sStorageTimeInterval = "";//存储时间间隔
        sSampTimeInterval = "";//采样间隔
        sUpReportTime = "";//上报间隔
        sConnMode = "";//总线通信模式
        sSerial_Baudrate = "";   //波特率
        sSerial_Com = "";     //数据位sSerial_Com.substring(0, 1)     停止位sSerial_Com.substring(2, 3)    校验位sSerial_Com.substring(1, 2)
        sMODBUS_deviceAdd = "";          //设备地址
        sMODBUS_functionCode = "";       //功能码
        sMODBUS_registerAdd = "";           //寄存器地址
        sMODBUS_registerNumber = "";         //寄存器个数
        sMODBUS_dataIntercept = "";         //数据截取
        sMODBUS_universalSerialPort = "";         //通用串口
        sElectricity_sensor = "";//电流传感器       ADC类型
        sMaxRange = "";//上量程
        sMinRange = "";//下量程
        sMaxValue = "";//最大值  输入电流
        sMinValue = "";//最小值  输入电流
        upperLimitValue1 = "";//上限值1
        upperLimitValue2 = "";//上限值2
        upperLimitValue3 = "";//上限值3
        upperLimitValueConnect = "";     //上限值报警内容
        upperLimitValueOfDo = "";     //上限值关联DO编码
        lowerLimitValue1 = "";//下限值1
        lowerLimitValue2 = "";//下限值2
        lowerLimitValue3 = "";//下限值3
        lowerLimitValueOfDo = "";     //下限值关联DO编码
        lowerLimitValueConnect = "";     //下限值报警内容

        threshold = "";//报警阈值
        diAlarm = "";     //报警触发方式
        sAlarmContext = "";//报警内容
        sDoRelate = "0";     //阈值DO关联

        dataBits = "";     // 数据位
        stopBits = "";     // 停止位
        checkBits = "";     // 校验位

        reportingInterval1 = "";//加报间隔1
        reportingInterval2 = "";//加报间隔2
        reportingInterval3 = "";//加报间隔3
        acquisitionInterval1 = "";//采集间隔1
        acquisitionInterval2 = "";//采集间隔2
        acquisitionInterval3 = "";//采集间隔3

        sensorMaxRange = "";//传感器上量程
        sensorMinRange = "";//传感器下量程


        /**
         * 报警参数界面
         */
        boolean cavIsChange = false;     //报警阈值、上限值1、上限值2、上限值3、下限值1、下限值2、下限值3、加报间隔1、加报间隔2、加报间隔3、   是否被修改过
        boolean addTimIsChange = false;     //采集间隔1、采集间隔2、采集间隔3
        boolean senOrrangeIsChange = false;     //传感器上量程、传感器下量程
        boolean diIsChange = false;     //报警触发方式、报警联动
        switch (hardWareInterface) {
            case "RS232-1":
            case "RS485-1":
            case "RS485-2":
            case "RS485-3":
            case "ADC0":
            case "ADC1":
            case "ADC2":
            case "ADC3":
            case "ADC4":
            case "ADC5":
            case "LORA":
            case "RainFall":
                for (ParamListData data : passWayList) {
                    switch (data.getName()) {
                        case "报警阈值":
                            threshold = TextUtils.isEmpty(data.getValue()) ? "": data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                            break;
                        case "上限值1":
                            upperLimitValue1 = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                        case "上限值2":
                            upperLimitValue2 = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                            break;
                        case "上限值3":
                            upperLimitValue3 = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                            break;
                        case "上限值报警内容":
                            upperLimitValueConnect = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                            break;
                        case "上限值关联DO":
                            upperLimitValueOfDo = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                            break;
                        case "下限值1":
                            lowerLimitValue1 = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                            break;
                        case "下限值2":
                            lowerLimitValue2 = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                            break;
                        case "下限值3":
                            lowerLimitValue3 = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                            break;
                        case "下限值报警内容":
                            lowerLimitValueConnect = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                            break;
                        case "下限值关联DO":
                            lowerLimitValueOfDo = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                            break;
                        case "阈值DO关联":
                            sDoRelate = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            cavIsChange = getDataIsChange(cavIsChange, data.getChange());
                            break;
                        case "报警内容":
                            sAlarmContext = TextUtils.isEmpty(data.getValue()) ? "": data.getValue();
                            break;
                        case "加报间隔1":
                            reportingInterval1 = TextUtils.isEmpty(data.getValue()) ? "": data.getValue();
                            addTimIsChange = getDataIsChange(addTimIsChange, data.getChange());
                            break;
                        case "加报间隔2":
                            reportingInterval2 = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            addTimIsChange = getDataIsChange(addTimIsChange, data.getChange());
                            break;
                        case "加报间隔3":
                            reportingInterval3 = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            addTimIsChange = getDataIsChange(addTimIsChange, data.getChange());
                            break;
                        case "采集间隔1":
                            acquisitionInterval1 = TextUtils.isEmpty(data.getValue()) ? "": data.getValue();
                            addTimIsChange = getDataIsChange(addTimIsChange, data.getChange());
                            break;
                        case "采集间隔2":
                            acquisitionInterval2 = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            addTimIsChange = getDataIsChange(addTimIsChange, data.getChange());
                            break;
                        case "采集间隔3":
                            acquisitionInterval3 = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            addTimIsChange = getDataIsChange(addTimIsChange, data.getChange());
                            break;
                        case "传感器上量程":
                            sensorMaxRange = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            senOrrangeIsChange = getDataIsChange(senOrrangeIsChange, data.getChange());
                            break;
                        case "传感器下量程":
                            sensorMinRange = TextUtils.isEmpty(data.getValue()) ? "" : data.getValue();
                            senOrrangeIsChange = getDataIsChange(senOrrangeIsChange, data.getChange());
                            break;
                        case "报警抓拍间隔":
                            if (data.getChange()) {
                                submitList.add(new ParamListData("报警抓拍间隔", HexUtil.chineseWordToGbk("AT+CHALMCAP=" + sPassageWay + "," + data.getValue()) + "0D0A", "AT+CHALMTXT"));
                            }
                            break;
                    }
                }
                break;
            case "DI0":
            case "DI1":
            case "DI2":
            case "DI3":
                for (ParamListData data : passWayList) {
                    switch (data.getName()) {
                        case "报警触发方式":
                            diAlarm = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            diIsChange = getDataIsChange(diIsChange, data.getChange());
                            break;
                        case "报警内容":
                            sAlarmContext = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (data.getChange()) {
                                submitList.add(new ParamListData("报警内容", HexUtil.chineseWordToGbk("AT+CATXT=" + sPassageWay + "," + sAlarmContext) + "0D0A", "AT+CATXT"));
                            }
                            break;
                        case "阈值DO关联":
                            sDoRelate = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            diIsChange = getDataIsChange(diIsChange, data.getChange());
                            break;
                    }
                }
                break;
        }

        if (diIsChange) {
            switch (diAlarm) {
                case "关闭":
                    submitList.add(new ParamListData("报警触发方式", HexUtil.chineseWordToGbk("AT+CHDINPORT=CH" + sPassageWay + "," + hardWareInterface + ",0," + sDoRelate) + "0D0A", "AT+CHDINPORT"));
                    break;
                case "低电平":
                    submitList.add(new ParamListData("报警触发方式", HexUtil.chineseWordToGbk("AT+CHDINPORT=CH" + sPassageWay + "," + hardWareInterface + ",1," + sDoRelate) + "0D0A", "AT+CHDINPORT"));
                    break;
                case "高电平":
                    submitList.add(new ParamListData("报警触发方式", HexUtil.chineseWordToGbk("AT+CHDINPORT=CH" + sPassageWay + "," + hardWareInterface + ",2," + sDoRelate) + "0D0A", "AT+CHDINPORT"));
                    break;
            }
        }
        if (cavIsChange) {
            submitList.add(new ParamListData("报警参数", HexUtil.chineseWordToGbk("AT+CAV=" + sPassageWay + "," + upperLimitValue1 + "," + upperLimitValue2 + "," + upperLimitValue3 + "," + upperLimitValueOfDo + "," + upperLimitValueConnect
                    + "," + lowerLimitValue1 + "," + lowerLimitValue2 + "," + lowerLimitValue3 + "," + lowerLimitValueOfDo + "," + lowerLimitValueConnect + "," + threshold + "," + sDoRelate + "," + sAlarmContext) + "0D0A", "AT+CAV"));
        }
        if (addTimIsChange) {
            submitList.add(new ParamListData("加报间隔和采集间隔", HexUtil.chineseWordToGbk("AT+ADDTIME=" + sPassageWay + "," + reportingInterval1 + "," + reportingInterval2 + "," + reportingInterval3 + "," + acquisitionInterval1 + "," + acquisitionInterval2 + "," + acquisitionInterval3) + "0D0A", "AT+CHALM"));
        }
        if (senOrrangeIsChange) {
            submitList.add(new ParamListData("传感器上下量程", HexUtil.chineseWordToGbk("AT+SENORRANGE=" + sPassageWay + "," + sensorMaxRange + "," + sensorMinRange) + "0D0A", "AT+CHALM"));
        }

        /**
         * 通道属性界面
         */
        boolean chparamIsChange = false;   //硬件接口,数据采样开光 , 上电延迟、通道标识、采集数据单位   是否修改
        boolean cbaseIsChange = false;   //采样基值、基值使用方式  是否修改
        switch (hardWareInterface) {
            case "无":
                submitList.add(new ParamListData("通道基本参数", HexUtil.chineseWordToGbk("AT+CCFG=" + sPassageWay + "," + "无") + "0D0A", "AT+CCFG"));
               break;
            default:
                for (ParamListData data : passWayList) {
                    switch (data.getName()) {
                        //通道基本参数
                        case "硬件接口":
                            sDeviceInface = hardWareInterface;
                            chparamIsChange = getDataIsChange(chparamIsChange, data.getChange());
                            break;
                        case "数据采样开关":
                            sChannelOnOff = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (sChannelOnOff.equals("关闭")) {
                                sChannelOnOff = "0";
                            } else {
                                sChannelOnOff = "1";
                            }
                            chparamIsChange = getDataIsChange(chparamIsChange, data.getChange());
                            break;
                        case "上电延迟":
                            sPowerOnDelay = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            chparamIsChange = getDataIsChange(chparamIsChange, data.getChange());
                            break;
                        case "通道标识":
                            sFactorName = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            chparamIsChange = getDataIsChange(chparamIsChange, data.getChange());
                            break;
                        case "采集数据单位":
                            sUnit = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            chparamIsChange = getDataIsChange(chparamIsChange, data.getChange());
                            break;
                        case "基值使用方法":
                            sSampBaseValueMethod = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            cbaseIsChange = getDataIsChange(cbaseIsChange, data.getChange());
                            break;
                        case "采样基值":
                            sSampBaseValue = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (!canParseInt(sSampBaseValue, hardWareInterface)) {
                                BasisApp.showToast("采样基值错误");
                                return false;
                            }
                            cbaseIsChange = getDataIsChange(cbaseIsChange, data.getChange());
                            break;
                        //采集数据精度
                        case "采集数据精度":
                            sPrecision = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (data.getChange()) {
                                submitList.add(new ParamListData("采集数据精度", HexUtil.chineseWordToGbk("AT+CPREC=" + sPassageWay + "," + sPrecision) + "0D0A", "AT+CPREC"));
                            }
                            break;
                        //数据存储间隔
                        case "数据存储间隔":
                            sStorageTimeInterval = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (!canParseInt(sStorageTimeInterval, hardWareInterface)) {
                                BasisApp.showToast("数据存储间隔错误");
                                return false;
                            }
                            if (data.getChange()) {
                                submitList.add(new ParamListData("数据存储间隔", HexUtil.chineseWordToGbk("AT+CSTOINT=" + sPassageWay + "," + sStorageTimeInterval) + "0D0A", "AT+CSTOINT"));
                            }
                            break;
                        //数据采样间隔
                        case "数据采样间隔":
                            sSampTimeInterval = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (!canParseInt(sSampTimeInterval, hardWareInterface)) {
                                BasisApp.showToast(data.getName() + "错误");
                                return false;
                            }

                            if (!TextUtils.isEmpty(sSampTimeInterval) && (Integer.valueOf(sSampTimeInterval) > 65535 || Integer.valueOf(sSampTimeInterval) < 5)) {
                                BasisApp.showToast("数据采样间隔请输入范围内的值");
                                return false;
                            }
                            if (data.getChange()) {
                                submitList.add(new ParamListData("数据采样间隔", HexUtil.chineseWordToGbk("AT+CSAMINT=" + sPassageWay + "," + sSampTimeInterval) + "0D0A", "AT+CSAMINT"));
                            }
                            break;
                        case "唤醒间隔":
                            awakeningInterval = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (data.getChange()) {
                                submitList.add(new ParamListData("唤醒间隔", HexUtil.chineseWordToGbk("AT+CWAKEINT=" + sPassageWay + "," + awakeningInterval) + "0D0A", "AT+CHPREC"));
                            }
                            break;
                        case "异常值阀值":
                            exceptionValue = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (data.getChange()) {
                                submitList.add(new ParamListData("异常值阀值", HexUtil.chineseWordToGbk("AT+CJUGEPOINT=" + sPassageWay + "," + exceptionValue) + "0D0A", "AT+CHPREC"));
                            }
                            break;
                    }

                }

                if (chparamIsChange) {
                    submitList.add(new ParamListData("通道基本参数", HexUtil.chineseWordToGbk("AT+CCFG=" + sPassageWay + "," + hardWareInterface + "," + sChannelOnOff + "," + sPowerOnDelay + "," + sFactorName + "," + sUnit) + "0D0A", "AT+CCFG"));
                }

                if (cbaseIsChange) {
                    switch (sSampBaseValueMethod) {
                        case "采集值+基值":
                            submitList.add(new ParamListData("采样基值", HexUtil.chineseWordToGbk("AT+CBASE=" + sPassageWay + "," + sSampBaseValue) + "0D0A", "AT+CBASE"));
                            break;
                        case "基值-采集值":
                            submitList.add(new ParamListData("采样基值", HexUtil.chineseWordToGbk("AT+CBASE=" + sPassageWay + ",-" + sSampBaseValue) + "0D0A", "AT+CBASE"));
                            break;
                        case "采集值-基值":
                            submitList.add(new ParamListData("采样基值", HexUtil.chineseWordToGbk("AT+CBASE=" + sPassageWay + ",@" + sSampBaseValue) + "0D0A", "AT+CBASE"));
                            break;
                    }
                }
                break;
        }
        /**
         * 通道参数
         */

        boolean chcomIsChange = false;     //ADC类型、上量程、下量程、最大值、最小值
        boolean bitsIsChange = false;     //波特率、数据位、停止位、校验位
        boolean chComModIsChange = false;     //设备地址、功能码、寄存器地址、寄存器个数、数据截取
        switch (hardWareInterface) {
            case "ADC0":
            case "ADC1":
            case "ADC2":
            case "ADC3":
            case "ADC5":
                for (ParamListData data : passWayList) {
                    switch (data.getName()) {
                        case "ADC类型":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            if ((TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue()).equals("电流")) {
                                sElectricity_sensor = "1";
                            } else {
                                sElectricity_sensor = "0";
                            }
                            chcomIsChange = getDataIsChange(chcomIsChange, data.getChange());
                            break;
                        case "上量程":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMaxRange = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            chcomIsChange = getDataIsChange(chcomIsChange, data.getChange());
                            break;
                        case "下量程":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMinRange = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            chcomIsChange = getDataIsChange(chcomIsChange, data.getChange());
                            break;
                        case "最大值":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMaxValue = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            chcomIsChange = getDataIsChange(chcomIsChange, data.getChange());
                            break;
                        case "最小值":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sMinValue = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            chcomIsChange = getDataIsChange(chcomIsChange, data.getChange());
                            break;
                    }
                    chcomIsChange = getDataIsChange(chcomIsChange, data.getChange());
                }
                break;
            case "RS232-1":
            case "RS485-1":
            case "RS485-2":
            case "RS485-3":
                for (ParamListData data : passWayList) {
                    switch (data.getName()) {
                        case "通信波特率":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            sSerial_Baudrate = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            break;
                        case "数据位":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            dataBits = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            break;
                        case "停止位":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            stopBits = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            break;
                        case "校验位":
                            if (TextUtils.isEmpty(TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue())) {
                                BasisApp.showToast(data.getName() + "的值不能为空");
                                return false;
                            }
                            checkBits = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            BleLog.d("checkBits ===" + checkBits);
                            switch (checkBits) {
                                case "无校验":
                                    checkBits = "N";
                                    break;
                                case "奇校验":
                                    checkBits = "O";
                                    break;
                                case "偶校验":
                                    checkBits = "E";
                                    break;
                            }
                            break;
                        case "总线通信模式":
                            sConnMode = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            break;
                        case "设备地址":
                            sMODBUS_deviceAdd = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (data.isEnable()) {
                                if (!canParseInt(sMODBUS_deviceAdd, hardWareInterface)) {
                                    BasisApp.showToast(data.getName() + "只能输入数字");
                                    return false;
                                }
                                if (Integer.valueOf(sMODBUS_deviceAdd) > 255 || Integer.valueOf(sMODBUS_deviceAdd) < 0) {
                                    BasisApp.showToast(data.getName() + "只能输入范围内的数字");
                                    return false;
                                }
                            }
                            break;
                        case "功能码":
                            sMODBUS_functionCode = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (data.isEnable()) {
                                if (Integer.valueOf(sMODBUS_functionCode) > 255 || Integer.valueOf(sMODBUS_functionCode) < 0) {
                                    BasisApp.showToast(data.getName() + "只能输入范围内的数字");
                                    return false;
                                }
                            }
                            break;
                        case "寄存器地址":
                            sMODBUS_registerAdd = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (data.isEnable()) {
                                if (TextUtils.isEmpty(data.getValue())) {
                                    if (!canParseInt(sMODBUS_registerAdd, hardWareInterface)) {
                                        BasisApp.showToast(data.getName() + "只能输入数字");
                                        return false;
                                    }
                                    if (Integer.valueOf(sMODBUS_registerAdd) > 65535 || Integer.valueOf(sMODBUS_registerAdd) < 0) {
                                        BasisApp.showToast(data.getName() + "只能输入范围内的数字");
                                        return false;
                                    }
                                }
                            }
                            break;
                        case "寄存器个数":
                            sMODBUS_registerNumber = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (data.isEnable()) {
                                if (TextUtils.isEmpty(data.getValue())) {
                                    if (!canParseInt(sMODBUS_registerNumber, hardWareInterface)) {
                                        BasisApp.showToast(data.getName() + "只能输入数字");
                                        return false;
                                    }
                                    if (Integer.valueOf(sMODBUS_registerNumber) > 255 || Integer.valueOf(sMODBUS_registerNumber) < 0) {
                                        BasisApp.showToast(data.getName() + "只能输入范围内的数字");
                                        return false;
                                    }
                                }
                            }
                            break;
                        case "数据截取":
                            sMODBUS_dataIntercept = data.getValue();
                            if (data.isEnable()) {
                                submitList.add(new ParamListData("数据截取", HexUtil.chineseWordToGbk("AT+MBDATAPARA=CH" + sPassageWay + "," + sMODBUS_dataIntercept) + "0D0A", "AT+MBDATAPARA"));
                            }
                            break;
                        case "通用串口":
                            sMODBUS_universalSerialPort = TextUtils.isEmpty(data.getValue()) ? data.getKey() : data.getValue();
                            if (TextUtils.isEmpty(sMODBUS_universalSerialPort)) {
                                sMODBUS_universalSerialPort = "";
                            }
                            break;
                    }
                    bitsIsChange = getDataIsChange(bitsIsChange, data.getChange());
                    chComModIsChange = getDataIsChange(chComModIsChange, data.getChange());
                }
                break;
        }
        if (chcomIsChange) {
            submitList.add(new ParamListData("通信参数", HexUtil.chineseWordToGbk("AT+CCOM=" + sPassageWay + "," + hardWareInterface + "," + sElectricity_sensor + "," + sMaxRange + "," + sMinRange + "," + sMaxValue + "," + sMinValue) + "0D0A", "AT+CHCOM"));
        }
        if (bitsIsChange) {
            submitList.add(new ParamListData("通信参数", HexUtil.chineseWordToGbk("AT+CCOM=" + sPassageWay + "," + hardWareInterface + "," + sSerial_Baudrate + "," + dataBits.charAt(0) + checkBits + stopBits.charAt(0)) + "0D0A", "AT+CHCOM"));
        }
        if (chComModIsChange) {
            switch (sConnMode) {
                case "透传":
                    sConnMode = "0";
                    submitList.add(new ParamListData("通道Modbus参数", HexUtil.chineseWordToGbk("AT+CCOMMOD=" + sPassageWay + "," + sConnMode) + "0D0A", "AT+CHCOMMOD"));
                    break;
                case "MODBUS":
                    sConnMode = "1";
                    submitList.add(new ParamListData("通道Modbus参数", HexUtil.chineseWordToGbk("AT+CCOMMOD=" + sPassageWay + "," + sConnMode + "," + sMODBUS_deviceAdd + "," + sMODBUS_functionCode + "," + sMODBUS_registerAdd + "," + sMODBUS_registerNumber) + "0D0A", "AT+CHCOMMOD"));
                    break;
                case "串口":
                    sConnMode = "2";
                    submitList.add(new ParamListData("通道Modbus参数", HexUtil.chineseWordToGbk("AT+CCOMMOD=" + sPassageWay + "," + sConnMode + "," + sMODBUS_universalSerialPort) + "0D0A", "AT+CCOMMOD"));
                    break;
                case "MODBUS_FLOAT":
                    sConnMode = "3";
                    submitList.add(new ParamListData("通道Modbus参数", HexUtil.chineseWordToGbk("AT+CCOMMOD=" + sPassageWay + "," + sConnMode + "," + sMODBUS_deviceAdd + "," + sMODBUS_functionCode + "," + sMODBUS_registerAdd + "," + sMODBUS_registerNumber) + "0D0A", "AT+CHCOMMOD"));
                    break;
                case "MODBUS_BCD":
                    sConnMode = "4";
                    submitList.add(new ParamListData("通道Modbus参数", HexUtil.chineseWordToGbk("AT+CCOMMOD=" + sPassageWay + "," + sConnMode + "," + sMODBUS_deviceAdd + "," + sMODBUS_functionCode + "," + sMODBUS_registerAdd + "," + sMODBUS_registerNumber) + "0D0A", "AT+CHCOMMOD"));
                    break;
            }
        }
        return true;
    }

    public static void setPassWayListChange(List<List<ParamListData>> allPassWayList) {
        for (List<ParamListData> dataList : allPassWayList) {
            for (ParamListData data : dataList) {
                data.setChange(false);
            }
        }
    }

    private static boolean getDataIsChange(boolean originalData, boolean dataIsChange) {
        if (originalData || dataIsChange) {
            return true;
        }
        return false;
    }

    public static boolean canParseInt(String str, String hardWareInterface) {
        if (TextUtils.isEmpty(str)) { //验证是否为空
            if (hardWareInterface.contains("DI")) {
                return true;
            }
            return false;
        }
        return str.matches("-?[0-9]+.*[0-9]*");
//        return str.matches("\\d+"); //使用正则表达式判断该字符串是否为数字，第一个\是转义符，\d+表示匹配1个或 //多个连续数字，"+"和"*"类似，"*"表示0个或多个
    }

    public static void hardwareInterfaceIsChange(String hardwareInterface, List<ParamListData> passWayList) {
        if(TextUtils.isEmpty(passWayList.get(0).getValue())  || !passWayList.get(0).getValue().equals(hardwareInterface)){
            passWayList.get(0).setValue(hardwareInterface);
            passWayList.get(0).setChange(true);
        }
    }

    private String returnBlank(String vale){
       if(TextUtils.isEmpty(vale)){
            return "";
        }
        return vale;
    }

}
